package cn.com.enboga.musicbox.sys
{
	import cn.com.enboga.musicbox.control.MusicControl;
	import cn.com.enboga.musicbox.control.MusicsListControl;
	import cn.com.enboga.musicbox.db.LocalDbConnection;
	import cn.com.enboga.musicbox.model.ModelLocator;
	import cn.com.enboga.musicbox.utils.OtherUtils;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class SystemManage
	{
		public static var xmlFile:File = File.applicationStorageDirectory.resolvePath("musicBoxSetting.xml");
		private var musicsListControl:MusicsListControl = new MusicsListControl();
		private var musicControl:MusicControl = new MusicControl();
		
		public function SystemManage()
		{
			
		}
		
		public function sysInit(musicBox:ModelLocator):void
		{
			var db:LocalDbConnection = new LocalDbConnection();
			db.createMusicBoxInfo();
			
			var xml:XML = readXML(xmlFile);
			musicBox._playing = OtherUtils.stringToBoolean(xml.children()[0]["playing"]);
			musicBox._playListId = xml.children()[0]["playListId"];
			musicBox._playIndex = xml.children()[0]["playIndex"];
			musicBox._position = xml.children()[0]["position"];
			
			musicBox._savePlayingState = OtherUtils.stringToBoolean(xml.children()[1]["savePlayingState"]);
			musicBox._playMode = xml.children()[1]["playMode"];
			musicBox._playCyc = xml.children()[1]["playCyc"];
			musicBox._volume = new Number(xml.children()[1]["volume"]);
			musicBox._transform.volume = musicBox._volume;
			
			musicsListControl.m3u();
			
			if(musicBox._playing)
			{
				var file:File = musicBox._musicInfoList[musicBox._playIndex] as File;
				musicControl.newPlay(file.url, musicBox._position);
			}
		}

		/** 读取XML文件返回XML */
		public static function readXML(file:File):XML{
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			
			var xml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();
			
			return xml;
		}
		
		/** 保存XML到XML文件 */
		public static function saveXMLFile(file:File, xml:XML):void{
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			
			var outputString:String = '<?xml version="1.0" encoding="UTF-8"?>\n';
			outputString += xml.toXMLString();
			fileStream.writeUTFBytes(outputString);
			
			fileStream.close();
		}
		
		/** 保持设置到XML */
		public static function saveXML(musicBox:ModelLocator):void{
			var xml:XML = readXML(xmlFile);
			xml.children()[0]["playing"] = musicBox._playing;
			xml.children()[0]["playListId"] = musicBox._playListId;
			xml.children()[0]["playIndex"] = musicBox._playIndex;
			xml.children()[0]["position"] = musicBox._position;
			
			xml.children()[1]["savePlayingState"] = musicBox._savePlayingState;
			xml.children()[1]["playMode"] = musicBox._playMode;
			xml.children()[1]["playCyc"] = musicBox._playCyc;
			xml.children()[1]["volume"] = musicBox._transform.volume;
			
			saveXMLFile(xmlFile, xml);
		}
	}
}